package com.example.demo.mvvm.utils;

import android.databinding.BindingAdapter;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.demo.mvvm.R;
import com.example.demo.mvvm.bean.BannerBean;
import com.example.demo.mvvm.bean.KnowBean;
import com.example.demo.mvvm.common.DividerLine;
import com.google.android.flexbox.FlexboxLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 80711 on 2018/5/16.
 */

public class RefreshUtil {

    @BindingAdapter({"refresh"})
    public static void onRefresh(SmartRefreshLayout layout , OnRefreshLoadmoreListener listener){
        layout.setOnRefreshLoadmoreListener(listener);
    }

    @BindingAdapter({"addItemDecoration"})
    public static void addItemDecoration(RecyclerView view, int type) {
        DividerLine dividerLine;
        switch (type) {
            case DividerLine.HORIZONTAL:
                dividerLine = new DividerLine(DividerLine.HORIZONTAL);
                dividerLine.setMarginLeft(20);
                view.addItemDecoration(dividerLine);
                break;

            case DividerLine.VERTICAL:
                dividerLine = new DividerLine(DividerLine.VERTICAL);
                dividerLine.setMarginLeft(20);
                view.addItemDecoration(dividerLine);
                break;
            case DividerLine.CROSS:
                dividerLine = new DividerLine(DividerLine.CROSS);
                view.addItemDecoration(dividerLine);
                break;

            case DividerLine.DEFAULT:
            default:
                break;
        }
    }

    @BindingAdapter("initBanner")
    public static void initBanner(Banner mBanner , List<BannerBean> bannerLists){
        if(bannerLists == null){
            return;
        }
        List<String> lists = new ArrayList<>();
        for(BannerBean bean : bannerLists){
            lists.add(bean.getTitle());
        }
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        mBanner.setImageLoader(new BannerImageLoader());
        mBanner.setImages(bannerLists);
        mBanner.setBannerTitles(lists);
        mBanner.setBannerAnimation(Transformer.Default);
        mBanner.isAutoPlay(true);
        mBanner.setDelayTime(3600);
        mBanner.setIndicatorGravity(BannerConfig.CENTER);
        mBanner.start();
    }

    @BindingAdapter("bannerListener")
    public static void setBannerListener(Banner mBanner ,  OnBannerListener listener){
        mBanner.setOnBannerListener(listener);
    }


    @BindingAdapter("addFlexItem")
    public static <T> void addFlexItem(FlexboxLayout layout , List<T> datas){
        layout.removeAllViews();
        for(T b : datas) {
            if(b instanceof KnowBean.ChildrenBean){
                KnowBean.ChildrenBean bean = (KnowBean.ChildrenBean) b;
                TextView textView = new TextView(ActivityUtils.peek());
                textView.setTextColor(ContextCompat.getColor(ActivityUtils.peek() , R.color.color_txt));
                textView.setTextSize( TypedValue.COMPLEX_UNIT_SP, 13);
                FlexboxLayout.LayoutParams  layoutParams = new FlexboxLayout.LayoutParams(FlexboxLayout.LayoutParams.WRAP_CONTENT , FlexboxLayout.LayoutParams.WRAP_CONTENT);
                layoutParams.setMargins(DensityUtil.dip2px(ActivityUtils.peek() , 10),0,DensityUtil.dip2px(ActivityUtils.peek() , 10),0);
                textView.setLayoutParams(layoutParams);
                textView.setText(bean.getName());
                layout.addView(textView);
            }

        }
    }

    @BindingAdapter("randomColor")
    public static void setTextColor(TextView textView , String color){
        try {
            textView.setTextColor(Color.parseColor(color));
        }catch (Exception e){
            textView.setTextColor(ContextCompat.getColor(textView.getContext() ,R.color.colorAccent ));
        }
    }

    @BindingAdapter("circleImage")
    public static void setCircleImage(ImageView image , Object path){
        if(path == null || TextUtils.isEmpty((String) path)){
            Glide.with(image.getContext()).load(R.mipmap.zuozu).transform(new GlideCircleTransform(image.getContext())).into(image);
        }else {
            Glide.with(image.getContext()).load(path).transform(new GlideCircleTransform(image.getContext())).into(image);
        }
    }

}
